<?php
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @license http://kontrollsoft.com/kontrollbase/userguide/general-LICENSE.php
   * @link http://kontrollbase.com
   */

$g['root'] = $root;
$nroot = substr_replace($root,"",-1); //remove the trailing slash from the root path

print<<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>KontrollKommand</title>

<link rel="stylesheet" type="text/css" href="$nroot/includes/style.css" />
<link rel="stylesheet" type="text/css" href="$nroot/includes/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="$nroot/includes/extjs/resources/css/xtheme-slate.css" />

<script type="text/javascript" src="$nroot/includes/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="$nroot/includes/extjs/ext-all-debug.js"></script>
HEAD;

print "\n
<script type=\"text/javascript\">
var templates = new Ext.data.SimpleStore({
  fields: ['id','name'],
  data: [";

foreach ($templates as $a) {
  print "['".$a['id']."','".$a['template_list_name']."'],\n";
}
print "['','']]});\n\n";

foreach($host as $key => $value) {
  foreach($value as $k => $v) {
    if($k == 'id') { $host_list_id=$v; }
    if($k == 'host_list_hostname') { $host_list_hostname=$v; }
    if($k == 'template_list_id') { $template_list_id=$v; }
    if($k == 'template_list_name') { $template_list_name=$v; }
  }
}

print<<<HEAD
Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
				expires: new Date(new Date().getTime()+(1000*60*60*24*7)), //7 days
				    }));

Ext.onReady(function(){
	      Ext.QuickTips.init();
	      Ext.form.Field.prototype.msgTarget = 'side';

	      var host = new Ext.FormPanel({ 
		renderTo: document.body,
		    bodyStyle:'padding:5px 5px 0',
		    buttonAlign: 'right',
		    width:390,
		    labelWidth:100,
		    url:'$nroot/index.php/edit/subhostbinding/', 
		    frame:true, 
		    title:'Edit Host Template Binding', 
		    defaultType:'textfield',
		    monitorValid:true,
		    items:[
			   {
			   fieldLabel:'host_list_id',
			       name:'host_list_id',
			       inputType: 'hidden',
			       value: '$host_list_id',
			       allowBlank:false
			       },
			   {
			   fieldLabel:'Hostname',
			       name:'host_list_hostname',
			       inputType: 'text',
			       width:250,
			       value: '$host_list_hostname',
			       allowBlank:false,
			       disabled: true
			       },
			   {
			   xtype: 'combo',
			       name: 'template_list_name',
			       fieldLabel: 'Template',
			       valueField: 'id',
			       hiddenName: 'template_list_id',
			       hiddenValue: '$template_list_id',
			       mode: 'local',
			       store: templates,
			       displayField: 'name',
			       width: 250,
			       emptyText:'Select Template',
			       typeAhead: true,
			       value: '$template_list_name',
			       triggerAction: 'all'
			       }
			   ],      
		    buttons:[
			     {
			     text:'Cancel',
				 handler:function(){
				 var redirect = '$nroot/index.php/main/hostbindings/';
				 window.location = redirect;
			       }
			     },
			     { 
			     text:'Edit Template Binding',
				 formBind: true, 
				 handler:function(){ 
				 host.getForm().submit({ 
				   method:'POST', 
				       waitTitle:'Connecting.', 
				       waitMsg:'Editing binding...',
				       setTimeout:10,
				       
				       success:function(){
				       var redirect = '$nroot/index.php/main/hostbindings/';
				       window.location = redirect;
				     },
				       
				       failure:function(form, action){ 
				       if(action.failureType == 'server'){ 
					 obj = Ext.util.JSON.decode(action.response.responseText); 
					 Ext.Msg.alert('Failed to edit binding.', obj.errors.reason); 
				       }else{ 
					 Ext.Msg.alert('Warning!', 'Update server is unreachable : ' + action.response.responseText); 
				       } 
				       host.getForm().reset(); 
				     } 
				   }); 
			       } 
			     }
			     ] 
		    });
	    });
</script>
HEAD;

print "</head>
<body>
</body>
</html>";

?>
